package com.sky.controller.user;


import com.github.pagehelper.Page;
import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.mapper.DishMapper;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userDishController")
@Slf4j
@RequestMapping("/user/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/list")
    public Result<List<DishVo>> list(Long categoryId) {
        log.info("查询菜品列表，分类id：{}", categoryId);
        String key = "dish_" + categoryId;
        List<DishVo> list = (List<DishVo>) redisTemplate.opsForValue().get(key);
        if(list!=null  && list.size()>0)
        {
            log.info("菜品列表从redis缓存中获取");

            return Result.success(list);
        }

        //查询起售的商品
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);

        List<DishVo> dishVoList = dishService.listWithFlavor(dish);
        redisTemplate.opsForValue().set(key,dishVoList);
        return Result.success(dishVoList);

    }
}
